﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;

namespace SmartMathLibrary.Management
{
    /// <summary>
    /// This class provides methods to send commands, like shutdown and reboot, to the operating system.
    /// </summary>
    [Serializable]
    public static class OperatingSystem
    {
        /// <summary>
        /// Reboots the operating system.
        /// </summary>
        public static void Reboot()
        {
            ManagementObject classInstance = new ManagementObject("root\\CIMV2",
                                                                  "Win32_OperatingSystem.ReplaceKeyPropery='ReplaceKeyPropertyValue'",
                                                                  null);

            ManagementBaseObject outParams =
                classInstance.InvokeMethod("Reboot", null, null);
        }

        /// <summary>
        /// Shutdowns the operating system.
        /// </summary>
        public static void Shutdown()
        {
            ManagementObject classInstance = new ManagementObject("root\\CIMV2",
                                                                  "Win32_OperatingSystem.ReplaceKeyPropery='ReplaceKeyPropertyValue'",
                                                                  null);

            ManagementBaseObject outParams =
                classInstance.InvokeMethod("Shutdown", null, null);
        }

        /// <summary>
        /// Hibernates the system, or requests that the system be hibernated.
        /// </summary>
        public static void Hibernate()
        {
            System.Windows.Forms.Application.SetSuspendState(System.Windows.Forms.PowerState.Hibernate, false, false);
        }

        /// <summary>
        /// Hibernates the system, or requests that the system be hibernated.
        /// </summary>
        /// <param name="force">True to force the suspended mode immediately; false to cause 
        /// Windows to send a suspend request to every application.</param>
        public static void Hibernate(bool force)
        {
            System.Windows.Forms.Application.SetSuspendState(System.Windows.Forms.PowerState.Hibernate, force, false);
        }

        /// <summary>
        /// Hibernates the system, or requests that the system be hibernated.
        /// </summary>
        /// <param name="force">True to force the suspended mode immediately; false to cause 
        /// Windows to send a suspend request to every application.</param>
        /// <param name="disableWakeEvent">true to disable restoring the system's power status 
        /// to active on a wake event, false to enable restoring the system's power status to active on a wake event.</param>
        public static void Hibernate(bool force, bool disableWakeEvent)
        {
            System.Windows.Forms.Application.SetSuspendState(System.Windows.Forms.PowerState.Hibernate, force,
                                                             disableWakeEvent);
        }

        /// <summary>
        /// Suspends the system, or requests that the system be suspended.
        /// </summary>
        public static void Suspend()
        {
            System.Windows.Forms.Application.SetSuspendState(System.Windows.Forms.PowerState.Suspend, false, false);
        }

        /// <summary>
        /// Suspends the system, or requests that the system be suspended.
        /// </summary>
        /// <param name="force">True to force the suspended mode immediately; false to cause 
        /// Windows to send a suspend request to every application.</param>
        public static void Suspend(bool force)
        {
            System.Windows.Forms.Application.SetSuspendState(System.Windows.Forms.PowerState.Suspend, force, false);
        }

        /// <summary>
        /// Suspends the system, or requests that the system be suspended.
        /// </summary>
        /// <param name="force">True to force the suspended mode immediately; false to cause 
        /// Windows to send a suspend request to every application.</param>
        /// <param name="disableWakeEvent">true to disable restoring the system's power status 
        /// to active on a wake event, false to enable restoring the system's power status to active 
        /// on a wake event.</param>
        public static void Suspend(bool force, bool disableWakeEvent)
        {
            System.Windows.Forms.Application.SetSuspendState(System.Windows.Forms.PowerState.Suspend, force,
                                                             disableWakeEvent);
        }
    }
}